﻿using Md.Api.Entity.ModelDB;
using PetaPoco.NetCore;

namespace Md.Petapoco.Repositories.MdTrainingEducation;
/// <summary>
/// 分类仓储
/// </summary>
public class MdClassifyRep : BaseRep<MdClassifyModel>
{
    /// <summary>
    /// 通过分类编号获取分类信息
    /// </summary>
    /// <param name="classifyId">分类编号</param>
    /// <returns>MdClassifyModel</returns>
    public MdClassifyModel GetMdClassifyByClassifyId(long? classifyId)
    {
        #region ===sql===
        string sqlStr = string.Format(@"
            select classify_id
                ,classify_name
                ,classify_icon
                ,classify_type
                ,classify_sort
                ,parent_id
                ,status
                ,del_flag
                ,create_by
                ,create_time
                ,update_by
                ,update_time
                ,remark
            from md_classify
            where 1 = 1 and classify_id = @0
        ");
        #endregion

        return MdTrainingEducationDB.GetInstance().SingleOrDefault<MdClassifyModel>(sqlStr, classifyId);
    }
    /// <summary>
    /// 获取分类列表数据
    /// </summary>
    /// <param name="orderBy">排序字段</param>
    /// <param name="isDesc">是否倒序</param>
    /// <param name="model">筛选条件</param>
    /// <returns>IEnumerable<MdClassifyModel></returns>
    public IEnumerable<MdClassifyModel> GetMdClassifyList(string? orderBy, bool? isDesc, MdClassifyModel model)
    {
        string sqlWhere = string.Empty;

        #region ===where===
        if (model.ClassifyId > 0)
        {
            sqlWhere += " and classify_id = @0 ";
        }
        if (!string.IsNullOrWhiteSpace(model.ClassifyName))
        {
            sqlWhere += " and classify_name like @1 ";
        }
        if (model.ClassifyType > 0)
        {
            sqlWhere += " and classify_type = @2 ";
        }
        if (model.ParentId > -1)
        {
            sqlWhere += " and parent_id = @3 ";
        }
        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlWhere += " and status = @4 ";
        }
        if (!string.IsNullOrWhiteSpace(model.DelFlag))
        {
            sqlWhere += " and del_flag = @5 ";
        }
        if (!string.IsNullOrWhiteSpace(model.CreateBy))
        {
            sqlWhere += " and create_by = @6 ";
        }
        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlWhere += " and update_by = @7 ";
        }
        #endregion

        #region ===sql===
        string sqlStr = string.Format(@"
            select classify_id
                ,classify_name
                ,classify_icon
                ,classify_type
                ,classify_sort
                ,parent_id
                ,status
                ,del_flag
                ,create_by
                ,create_time
                ,update_by
                ,update_time
                ,remark
            from md_classify
            where 1 = 1 {0} {1}
         ", sqlWhere, GetOrderByDesc(orderBy, isDesc));
        #endregion

        return MdTrainingEducationDB.GetInstance().Query<MdClassifyModel>(
            sqlStr,
            model.ClassifyId,
            string.Format(@"%{0}%", model.ClassifyName),
            model.ClassifyType,
            model.ParentId,
            model.Status,
            model.DelFlag,
            model.CreateBy,
            model.UpdateBy);
    }
    /// <summary>
    /// 分页获取分类列表数据
    /// </summary>
    /// <param name="orderBy">排序字段</param>
    /// <param name="isDesc">是否倒序</param>
    /// <param name="pageIndex">当前页</param>
    /// <param name="pageSize">每页条数</param>
    /// <param name="model">筛选条件</param>
    /// <returns>Page<MdClassifyModel></returns>
    public Page<MdClassifyModel> GetMdClassifyPageList(string? orderBy, bool? isDesc, int pageIndex, int pageSize, MdClassifyModel model)
    {
        string sqlWhere = string.Empty;

        #region ===where===
        if (model.ClassifyId > 0)
        {
            sqlWhere += " and classify_id = @0 ";
        }
        if (!string.IsNullOrWhiteSpace(model.ClassifyName))
        {
            sqlWhere += " and classify_name like @1 ";
        }
        if (model.ClassifyType > 0)
        {
            sqlWhere += " and classify_type = @2 ";
        }
        if (model.ParentId > -1)
        {
            sqlWhere += " and parent_id = @3 ";
        }
        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlWhere += " and status = @4 ";
        }
        if (!string.IsNullOrWhiteSpace(model.DelFlag))
        {
            sqlWhere += " and del_flag = @5 ";
        }
        if (!string.IsNullOrWhiteSpace(model.CreateBy))
        {
            sqlWhere += " and create_by = @6 ";
        }
        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlWhere += " and update_by = @7 ";
        }
        #endregion

        #region ===sql===
        string sqlStr = string.Format(@"
            select classify_id
                ,classify_name
                ,classify_icon
                ,classify_type
                ,classify_sort
                ,parent_id
                ,status
                ,del_flag
                ,create_by
                ,create_time
                ,update_by
                ,update_time
                ,remark
            from md_classify
            where 1 = 1 {0} {1}
         ", sqlWhere, GetOrderByDesc(orderBy, isDesc));
        #endregion

        return MdTrainingEducationDB.GetInstance().Page<MdClassifyModel>(
            pageIndex,
            pageSize,
            sqlStr,
            model.ClassifyId,
            string.Format(@"%{0}%", model.ClassifyName),
            model.ClassifyType,
            model.ParentId,
            model.Status,
            model.DelFlag,
            model.CreateBy,
            model.UpdateBy);
    }
    /// <summary>
    /// 更新分类
    /// </summary>
    /// <param name="model">请求实体</param>
    /// <returns>int</returns>
    public long UpdateMdClassify(MdClassifyModel model)
    {
        var setList = new List<string>();

        #region ===set===
        setList.Add(" classify_name = @2 ");

        setList.Add(" classify_icon = @3 ");

        setList.Add(" classify_type = @4 ");

        setList.Add(" classify_sort = @5 ");

        setList.Add(" parent_id = @6 ");

        setList.Add(" status = @7 ");

        setList.Add(" update_by = @8 ");

        setList.Add(" update_time = @9 ");

        setList.Add(" remark = @10 ");
        #endregion

        #region ===sql===
        string sqlStr = string.Format(@"
                set {0}
                where 1 = 1 and del_flag = @0 and classify_id = @1
         ", string.Join(",", setList));
        #endregion

        var res = MdTrainingEducationDB.GetInstance().Update<MdClassifyModel>(
            sqlStr,
            model.DelFlag,
            model.ClassifyId,
            model.ClassifyName,
            model.ClassifyIcon,
            model.ClassifyType,
            model.ClassifySort,
            model.ParentId,
            model.Status,
            model.UpdateBy,
            model.UpdateTime,
            model.Remark
         );

        return res;
    }
}

